package com.tianzmp.ttq.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.AccessibleElementId;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPRow;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPTableBody;
import com.itextpdf.text.pdf.PdfPTableHeader;
import com.itextpdf.text.pdf.PdfWriter;
import com.tianzmp.ttq.admin.util.ResourceUtils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhumengping
 *
 *
 * //            String resourcePath = ResourceUtils.getInstance().getResourcePath("fonts/simfang.ttf");
 * //            BaseFont baseFont = BaseFont.createFont(resourcePath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
 * //            Font font = new Font(baseFont, 14, Font.NORMAL, BaseColor.BLACK);
 *
 * @title: PdfTestMain
 * @projectName ttq-admin
 * @date 2023/10/23 18:25
 */
public class PdfTestMain {

    public static void main(String[] args) {
        List<String> datas = new ArrayList<>();


        String str = "100";
        StringBuffer sb = new StringBuffer("00010157");
        for (int i=0; i<32; i++){
            str = addNum(str);
            StringBuffer stringBuffer = new StringBuffer();
            String s = stringBuffer.append(str).append(sb).toString();
            datas.add(s);
        }

        int pageNo = 1;
        int pageSize = 16;
        //总页数
        int totalPageNo = (datas.size() + pageSize - 1) / pageSize;
        for (int i = 0; i< totalPageNo; i++){
            int start = pageNo <= 1 ? 0 : (pageNo - 1) * pageSize;
            List<String> newAimTplIds = datas.stream().sorted().skip(start).limit(pageSize).collect(Collectors.toList());
            System.out.println(JSONObject.toJSONString(newAimTplIds));
            PdfTestMain.getCode(newAimTplIds);
            pageNo +=1;
        }
    }



    public static void getCode(List<String> datas){
        PdfPTable table = new PdfPTable(4);
        table.setWidthPercentage(60.0f);
        table.setHorizontalAlignment(Element.ALIGN_CENTER);
        Document document = new Document(PageSize.A4);
        try {
            PdfWriter.getInstance(document, new FileOutputStream(new File(System.currentTimeMillis()+".pdf")));
            //打开文档
            document.open();
            for (int i=0; i<datas.size(); i++){
                File tempFile = File.createTempFile("tempFile", ".png");
                QrCode.getDataMatrixCode(datas.get(i),tempFile.getAbsolutePath());
                Image instance = Image.getInstance(tempFile.getAbsolutePath());
                PdfPCell pdfPCell = new PdfPCell(instance);
                //水平居中
                pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                //垂直居中
                pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);
                //隐藏边框
                pdfPCell.disableBorderSide(15);
                //每个单元格的高度
                pdfPCell.setFixedHeight(65.0f);
                table.addCell(pdfPCell);
                //删除临时文件
                tempFile.delete();
            }
            document.add(table);
            document.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }



    /**
     * <pre>
     *     写图片
     * </pre>
     *
     * @param outPutFilePath 必须是.PDF 结果
     * @return void
     * @author zhump
     * @date 2023/10/24 10:44
     */
    public static void writeImage(String outPutFilePath) {
        Document document = new Document();
        try {
            //创建一个pdf写入器,将文档写入到文件中
            PdfWriter.getInstance(document, new FileOutputStream(new File(outPutFilePath)));
            //打开文档
            document.open();

            String s = ResourceUtils.getInstance().getResourcePath("img");
            String filePath = s + File.separator + System.currentTimeMillis() + ".png";
            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            System.out.println(filePath);
            //生成图
            QrCode.createQrCode1("6400010157", filePath);
            Image instance = Image.getInstance(filePath);
            document.add(instance);
            //关闭文档
            document.close();
            System.out.println("pdf文件创建成功");
        } catch (FileNotFoundException e) {
            System.out.println("文件找不到");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("创建pdf文件失败");
            e.printStackTrace();
        }
    }

    public static String addNum(String str){
        String numStr = str.substring(str.length() - 2);
        int n = numStr.length();
        int num = Integer.parseInt(numStr) + 1;
        String added = String.valueOf(num);
        n = Math.min(n,added.length());
        CharSequence charSequence = str.subSequence(0, str.length() - n);
        return str = charSequence + added;
    }
}
